// pages/write-off/order-list.js
const format = require('../../utils/format.js')
const numeral = require("../../vendor/numeral/numeral.min")
const app = getApp()
const pool = new Map()
const statusObj = {
    "0": "待付款",
    "12": "已支付",
    "100": "已完成",
    "-1": "已退款",
}
Page({

    data: {
        status: null
    },

    onLoad: function(options) {
        this.setData(options)
        let tabs = app.write_off_tab
        this.setData({
            statusObj,
            tabs
        })
        this.loadData()
    },

    clickTab(evt) {
        const status = evt.currentTarget.dataset.status
        if (status != this.data.status) {
            this.setData({
                status
            })
            let tab = pool.get(this.data.status)
            if (tab && tab.list.length == 0) {
                tab.page = 1
                tab.nextPage = false
            }
            this.loadData()
        }
    },
    loadData() {
        let tab = pool.get(this.data.status)
        if (!tab) {
            tab = {
                page: 1,
                nextPage: true,
                list: []
            }
            pool.set(this.data.status, tab)
        }
        this.setData(tab)
        if (!tab.nextPage) {
            this.setData(tab)
            return
        }

        if (this.data.loading) {
            return
        }

        this.setData({
            loading: true
        })
        let data = {}
        if (this.data.status == null) {
            data = {
                page: tab.page
            }
        } else {
            data = {
                state: this.data.status,
                page: tab.page
            }
        }
        app.http.get("hx-order/order-index", {
            data
        }).then(res => {
            tab.list.push(...res.data.list.map(o => {
                o.payed = numeral(o.payed / 100).format("0,0.00")
                o.total_amount = numeral(o.total_amount / 100).format("0,0.00")
                let date = new Date(o.created_at * 1000)
                o.created_at = format.dateformat.format(date)
                return o
            }))
            tab.page++
                tab.nextPage = res.data.next_page
            this.setData(tab)
            wx.hideLoading()
            wx.stopPullDownRefresh()
            this.setData({
                loading: false
            })
        }).catch(err => {
            app.wxToast.error(err.message)
            this.setData({
                loading: false
            })
        })
    },
    /**
     * 重新支付
     */
    payAgain(evt) {
        let order_id = evt.currentTarget.dataset.orderid
        if (this.data.paying) return
        this.setData({
            paying: true
        })
        wx.showLoading({
            title: '请稍等',
        })
        app.http.post('pay/order', {
            data: {
                order_id
            }
        }).then(res => {
            wx.hideLoading()
            let jsPay = res.data.js
            if (res.data.pay_type == 2) {
                wx.showToast({
                    icon: 'none',
                    title: '支付成功',
                })
                this.onPullDownRefresh()
            }
            if (jsPay) {
                const obj = Object.assign(jsPay, {
                    success: res => {
                        wx.showToast({
                            icon: 'none',
                            title: '支付成功',
                        })
                        this.onPullDownRefresh()
                    },
                    fail: err => {
                        wx.showToast({
                            icon: 'none',
                            title: '支付取消',
                        })
                    }
                });
                wx.requestPayment(obj)
            }
            this.setData({
                paying: false
            })
        }).catch(err => {
            console.log(err.message)
            app.wxToast.error(err.message)
            this.setData({
                paying: false
            })
        })
    },
    /**
     * 删除
     */
    deleteOrder(evt) {
        let order_id = evt.currentTarget.dataset.orderid
        wx.showLoading({
            title: '请稍等',
        })
        app.http.post('hx-order/delete', {
            data: {
                order_id
            }
        }).then(res => {
            if (res.data.res) {
                wx.showToast({
                    icon: 'none',
                    title: '删除订单成功',
                })
                this.onPullDownRefresh()
            } else {
                wx.showToast({
                    icon: 'none',
                    title: '删除订单失败',
                })
            }
        })
    },
    onPullDownRefresh: function() {
        let tab = pool.get(this.data.status)
        tab.page = 1
        tab.nextPage = true
        tab.list = []
        this.loadData()
    },

    onReachBottom: function() {
        this.loadData()
    },

})